package com.wjm.blog.common.util;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Random;

/**
 * @author scxs
 * @description 生成序列编码的工具类
 *
 */
public class SerialCodeUtil {

	private static ThreadLocal<DateFormat> tl = new ThreadLocal<DateFormat>() {
		@Override
		public SimpleDateFormat initialValue() {
			return new SimpleDateFormat("yyyyMMdd");
		}
	};

	public static String createDateCode() {
		String format = tl.get().format(new Date());
		return format;
	}

	/**
	 * 创建定长数字字符串
	 */
	public static String createNumberCode(int length) {
		String format = tl.get().format(new Date());
		String str = "0123456789";
		Random random = new Random();
		StringBuffer sb = new StringBuffer();
		for (int i = 0; i < length; i++) {
			int number = random.nextInt(10);
			sb.append(str.charAt(number));
		}
		return format + sb.toString();
	}

	/**
	 * 创建定长数字字符串
	 */
	public static String createRequestId(int length) {
		String str = "0123456789";
		Random random = new Random();
		StringBuffer sb = new StringBuffer();
		for (int i = 0; i < length; i++) {
			int number = random.nextInt(10);
			sb.append(str.charAt(number));
		}
		return sb.toString();
	}
}
